package jsyf.oa.web.app;

import com.alibaba.fastjson.JSONObject;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.app.DriveFacade;

@Api(value = "行车记录接口", description = "行车记录接口")
@Controller
@RequestMapping("/drive")
public class DriveController {
    @Resource
    private DriveFacade driveFacade;
    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "保存里程",
            notes = "保存里程 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{\n" +
            "userId:\"2017121820434803834AA7E354A82ABB\",\n" +
            "createTime:\"2018-04-05 09:37:08\",\n" +
            "startTime:\"2018-04-05 09:33:51\",\n" +
            "driveTime:\"00:03:12\",\n" +
            "distance:\"0.3\"\n" +
            "}", dataType = "String", paramType = "body")
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String id = driveFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @ApiImplicitParam(name = "jsonData", value = "{userId:\"2017121820434803834AA7E354A82ABB\",date:\"2018-04\"}", dataType = "String", paramType = "body")
    @RequestMapping(value = "/getMonthDistance.json", method = RequestMethod.POST)
    public void getMonthDistance(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = driveFacade.getMonthDistance(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }
}
